from django.db import models
from django.utils.safestring import mark_safe
from markdown import markdown
from tinymce.models import HTMLField

from myapp.models import User


# Create your models here.
class Article(models.Model):
    title = models.CharField(max_length=100, null=False, verbose_name='文章标题')
    desc = models.CharField(max_length=256, null=False, verbose_name='文章概述')
    content = models.TextField(verbose_name='文章内容')
    save_number = models.IntegerField(verbose_name='收藏量', default=0)
    publish_date = models.DateTimeField(auto_now_add=True, verbose_name='发布日期')
    user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name='作者')

    def get_content_as_markdown(self):
        return mark_safe(markdown(self.content, safe_mode='escape'))

    class Meta:
        db_table = 'article'
        verbose_name = '文章表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class BlogPost(models.Model):
    title = models.CharField(max_length=100, null=False, verbose_name='文章标题')
    body = HTMLField(verbose_name='内容')
    save_number = models.IntegerField(verbose_name='收藏量', default=0)
    publish_date = models.DateTimeField(auto_now_add=True, verbose_name='发布日期')
    user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name='作者')

    class Meta:
        db_table = 'blogpost'
        verbose_name = '帖子表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title
